package org.diysearch.models;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public abstract class BaseModel {
	private static final Log LOG = LogFactory.getLog(BaseModel.class);

	public String toString() {
		return (new ReflectionToStringBuilder(this) {
			protected boolean accept(Field f) {
				return super.accept(f);
			}
		}).toString();
	}

	public Map<String, Object> toMap() {
		Map<String, Object> map = new HashMap<String, Object>();
		Field[] fields = this.getClass().getFields();
		for (Field f : fields) {
			try {
				Method method = this.getClass().getMethod(
						"get" + StringUtils.capitalize(f.getName()),
						new Class[] {});
				if (method != null) {
					Object val = method.invoke(this,new Object[] {});
					if (val != null) {
						map.put(f.getName(), val);
					}
				}
			} 
			catch (NoSuchMethodException ex) {} 
			catch (InvocationTargetException ex) {} 
			catch (IllegalAccessException ex) {
				
			}
		}
		return map;
	}

	public abstract String getAliasName();
}
